package com.car.action.admin;

import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;

import com.car.common.model.CardDiscuss;
import com.car.common.model.MessageUnreadNum;
import com.car.common.model.Prop;
import com.car.common.model.PropRecord;
import com.car.common.model.Resume;
import com.car.common.model.UserFriends;
import com.car.common.model.Users;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.utils.PicUtil;
import com.utils.StringUtil;
import com.utils.SystemClock;

public class PropController  extends Controller  {

	public void index() {
		List<Prop> propList = Prop.dao.find("select p.*,COUNT(r.id) as cnt from prop as p  LEFT JOIN  prop_record as r on p.id = r.prop_id where p.status = 1 GROUP BY p.id order by cnt desc  ");
		setAttr("list", propList);
		render("index.html");
	}
	
	public void history() {
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        } 
        String sql = "select p.name,p.icon ,r.*,u.nick_name as uname,FROM_UNIXTIME(r.createtime,'%Y-%m-%d %H:%i') as create_time_string " ;
        String andSql = " from prop_record as r left join prop as p on p.id = r.prop_id left join users as u on u.id = r.target_user_id order by id desc ";
        Page<PropRecord> recordPage = PropRecord.dao.paginate(page, 15, sql,andSql);
        setAttr("page", recordPage);
		render("history.html");
	}
	
	
	public void one() {
		render("one.html");
	}
	
	public void friend() {
		PropRecord record = PropRecord.dao.findById(getPara("id"));
		Integer user_id = record.getTargetUserId();
		Integer target_user_id = record.getUserId();
		 //判断是否是好友
		UserFriends one = UserFriends.dao.findFirst("select id from user_friends where (user_id = "+ user_id +" and target_user_id = " + target_user_id +") and status = 1 and delete_time is null order by id desc limit 1 ");
		if(one == null) {
			one = new UserFriends();
			one.setUserId(user_id.intValue());
			one.setTargetUserId(target_user_id);
			one.setStatus(1);
			one.setCreateTime(SystemClock.now()/1000L);
			one.setUpdateTime(SystemClock.now()/1000L);
			if (one.save()) {
				MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_FRIEND, target_user_id);
			}
			renderText("操作成功!");
		} else {
			renderText("已经是好友了");
		}
	} 
	
	/*
	public void test() {
		for(int i = 100000 ; i < 170000 ; i ++) {
			Users users = Users.dao.findById(i);
			if (users != null && !StringUtil.empty(users.getUserHeader())) {
				try {
					HttpClient client = new HttpClient();
			    	client.getParams().setContentCharset("UTF-8");
			    	PostMethod postMethod = new PostMethod(PicUtil.QINIU_DIR + users.getUserHeader());
			    	
			    	int status = client.executeMethod(postMethod);
					if (status == HttpStatus.SC_OK) {
						//String result = postMethod.getResponseBodyAsString();
					} else {
						Db.update("update users set user_header = null,is_true = 1 where id = " + users.getId());
						System.out.println(users.getId());
					}
					//String result = HttpRequestUtil.sendGet(PicUtil.QINIU_DIR + "java_admin_02e1f3a9c1674ac696076c1835d6a1cc.jpg");
				} catch (Exception e) {
					System.out.println(users.getId());
				}
			}
		}
		renderNull();
	}
	*/
}
